﻿namespace Anurgath.Shoutbox
{
    #region Usings
    using Anurgath.Shoutbox.Models;
    using Anurgath.Shoutbox.Services;
    using Microsoft.AspNet.SignalR;
    using Orchard;
    using Orchard.Environment;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Transactions;
    using System.Web;
    #endregion

    public class ShoutboxHub : Hub
    {
        #region Private variables
        private readonly Work<IOrchardServices> _OrchardServices;
        private readonly IHelpers _Helpers;
        private readonly IShoutboxService _ShoutboxService;
        private readonly IPostService _PostService;
        #endregion

        #region Constructors
        public ShoutboxHub(Work<IOrchardServices> orchardServices, IHelpers helpers, IShoutboxService shoutboxService, IPostService postService)
        {
            if (orchardServices == null)
            {
                throw new ArgumentNullException("orchardServices");
            }

            if (helpers == null)
            {
                throw new ArgumentNullException("helpers");
            }

            if (postService == null)
            {
                throw new ArgumentNullException("postService");
            }

            if (shoutboxService == null)
            {
                throw new ArgumentNullException("shoutboxService");
            }

            _OrchardServices = orchardServices;
            _Helpers = helpers;
            _ShoutboxService = shoutboxService;
            _PostService = postService;
        }
        #endregion

        #region Methods
        public void SendPublishPost(int shoutboxId, string content)
        {
            if (_OrchardServices.Value.WorkContext.CurrentUser == null)
            {
                return;
            }

            int currentUserId = _OrchardServices.Value.WorkContext.CurrentUser.Id;
            
            ShoutboxPart shoutboxPart = _ShoutboxService.GetShoutboxById(shoutboxId);

            PostPart PublishedPost = null;

            if (shoutboxPart != null)
            {
                PublishedPost = _PostService.PublishPost(currentUserId, shoutboxPart, content);
                _OrchardServices.Value.ContentManager.Flush();

                // Clients.All.receiveGetAllPosts(MyPreparePostsToSend(shoutboxId));
                Clients.All.receiveGetSinglePost(MyPostToSend(PublishedPost));
            }                       
        }

        public void SendPing()
        {
            Clients.Caller.receivePong();
        }

        public void SendGetAllPostsForShoutboxId(int shoutboxId)
        {            
            Clients.Caller.receiveGetAllPosts(MyPreparePostsToSend(shoutboxId));
        }
        #endregion

        #region My methods
        private List<object> MyPreparePostsToSend(int shoutboxId)
        {
            var ShoutboxPart = _ShoutboxService.GetShoutboxById(shoutboxId);

            var AllPosts = _PostService.GetAllPostsForShoutbox(shoutboxId).Reverse().Take(ShoutboxPart.HowManyPostsToShow).Reverse();

            List<object> PostsList = new List<object>();

            foreach (var Post in AllPosts)
            {
                PostsList.Add(MyPostToSend(Post));
            }

            return PostsList;
        }

        private object MyPostToSend(PostPart postPart)
        {
            var PostToSend = new
            {
                AddDate = postPart.AddDate,
                Content = postPart.Content,
                UserId = postPart.UserId,
                UserName = _Helpers.GetUsernameFromId(postPart.UserId)
            };

            return PostToSend;
        }
        #endregion
    }
}